#!/usr/bin/env python
# encoding: utf-8
"""
@Author: Beam
@Mail:506556658@qq.com
@file: gevent_socket_server.py
@time: 2017/5/13 9:45
"""
'''并发100个socket连接'''
import socket
import threading
import time
'''起100个线程，每个线程建立一个socket连接，每个连接不断循环的发送和接收数据'''
def sock_conn(count):
    client = socket.socket()
    client.connect(("localhost",8001))   #建立socket连接
    while True:
        client.send(("Hello %s" %count).encode("utf-8"))
        data = client.recv(1024)
        print("[%s] recv from server:"%threading.get_ident(),data.decode()) #threading.get_ident(返回当前线程的“线程标识符”
        time.sleep(2)
    client.close()

for i in range(100):
    t = threading.Thread(target=sock_conn,args=(i,))
    t.start()
